package org.smile.json.format;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.util.Date;

import org.smile.Smile;
import org.smile.util.DateUtils;

/**
 * 默认序列化配置
 */
public class DefaultSerializeConfig implements SerializeConfig {
    /**
     * 是否空属性可见
     */
    protected boolean nullValueView = false;
    //长整型转字段串
    protected byte longToStr = Smile.config.getByte(Smile.JSON_SERIALIZE_LONG_TO_STR,(byte)0);

    static  long JS_SAFE_NUMBer=(long)(Math.pow(2,53)-1.0);

    public DefaultSerializeConfig() {

    }
    /**
     * 是否空属性可见
     * @param nullValueView
     */
    protected DefaultSerializeConfig(boolean nullValueView) {
        this.nullValueView = nullValueView;
    }

    public void setNullValueView(boolean nullValueView) {
        this.nullValueView = nullValueView;
    }

    @Override
    public boolean isNullValueView() {
        return nullValueView;
    }

    @Override
    public String formatDate(Date date) {
        return DateUtils.defaultFormat(date);
    }

    @Override
    public String formatSqlDate(java.sql.Date date) {
        return DateUtils.formatOnlyDate(date);
    }

    @Override
    public boolean ignore(PropertyDescriptor pd, Object value) {
        return false;
    }

    @Override
    public boolean ignore(Field field, Object value) {
        return false;
    }

    @Override
    public boolean isLongToStr(Long value) {
        if(longToStr==FALSE) {
            return false;
        }
        if(longToStr==AUTO){
            if(value>JS_SAFE_NUMBer){
                return true;
            }
            return  false;
        }
        if(longToStr==TRUE){
            return true;
        }
        return false;
    }
}
